package com.distribution.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.core.page.TableDataInfo;
import com.distribution.common.exception.ServiceException;
import com.distribution.framework.config.ServerConfig;
import com.distribution.system.domain.entity.CmccDaAreaFlow;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.domain.req.CmccDaAreaFlowReq;
import com.distribution.system.domain.req.CmccDaProductListReq;
import com.distribution.system.service.CmccDaAreaFlowService;
import com.distribution.system.service.CmccDaProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 地区分流号卡增删改成操作
 *
 * @author
 * @module
 * @since 2023-05-20 20:46:37
 */
@RestController
@RequestMapping(value = "/cmccDaAreaFlow")
@Slf4j
public class CmccDaAreaFlowController extends BaseController {
    @Resource
    CmccDaAreaFlowService cmccDaAreaFlowService;

    @Autowired
    private CmccDaProductService cmccDaProductService;

    @PostMapping("/getAreaFlowInfoListByProductId")
    public List<CmccDaAreaFlow> getAreaFlowInfoListByProductId(@RequestBody Integer productId){
        //System.out.println("请求参数：" + id);
        //System.out.println(cmccDaAreaFlowService.getFlowCardById(id));
        return cmccDaAreaFlowService.getAreaFlowInfoListByProductId(productId);
    }

    /**
     * 号卡列表查询
     *
     * @return 号卡列表
     */
    @PostMapping("/getAreaFlowProductList")
    public TableDataInfo getAreaFlowProductList(@RequestBody CmccDaProductListReq cmccDaProductListReq) {
        List<CmccDaProduct> productListByQueryParam = cmccDaProductService.getProductListByQueryParam(cmccDaProductListReq);
        List<CmccDaAreaFlow> cmccDaDistanceFlowList = cmccDaAreaFlowService.list();
        //根据cardid去重
        List<Integer> idList = cmccDaDistanceFlowList.stream().map(CmccDaAreaFlow::getProductId).collect(Collectors.toList());
        List<CmccDaProduct> cmccDaProductList = productListByQueryParam.stream().filter(x -> idList.contains(x.getId())).collect(Collectors.toList());
        if (cmccDaProductList.isEmpty()) throw new ServiceException("查询不到分流信息");
        startPage();
        return getDataTable(cmccDaProductList);
    }

    /**
     * 添加地区分流号卡
     *
     * @param cmccDaAreaFlowReqList 地区分流号卡请求实例
     * @return 请求状态
     */
    @PostMapping("/add")
    AjaxResult addList(@RequestBody @Valid List<CmccDaAreaFlowReq> cmccDaAreaFlowReqList) {
        for (CmccDaAreaFlowReq cmccDaAreaFlowReq : cmccDaAreaFlowReqList) {
            cmccDaAreaFlowService.addAreaFlowInfo(cmccDaAreaFlowReq.getCurrentId(),cmccDaAreaFlowReq.getAreaFlowId(),
                    cmccDaAreaFlowReq.getProvinceCode(),cmccDaAreaFlowReq.getCityCode());
        }
        return success();
    }

    /**
     * 添加地区分流号卡
     *
     * @param cmccDaAreaFlowReq 地区分流号卡请求实例
     * @return 请求状态
     */
    @PostMapping("/update")
    AjaxResult update(@RequestBody @Valid CmccDaAreaFlowReq cmccDaAreaFlowReq) {
        //System.out.println(cmccDaAreaFlowReq);
        cmccDaAreaFlowService.updateAreaFlow(cmccDaAreaFlowReq.getCurrentId(), cmccDaAreaFlowReq.getAreaFlowId(),cmccDaAreaFlowReq.getProvinceCode(),cmccDaAreaFlowReq.getCityCode());
        return success();
    }

    /**
     * 取消分流
     *
     * @param productId 分流商品id
     * @return 请求状态
     */
    @PostMapping("/cancelAreaFlowInfoByProductId")
    AjaxResult cancelDistanceFlowByProductId(@RequestBody Integer productId) {
        //System.out.println(productId);
        List<CmccDaAreaFlow> list = cmccDaAreaFlowService.list(new LambdaQueryWrapper<CmccDaAreaFlow>().eq(CmccDaAreaFlow::getProductId, productId));
        if (list == null || list.isEmpty()) return error("数据不存在");
        List<Integer> idList = list.stream().map(CmccDaAreaFlow::getId).collect(Collectors.toList());
        cmccDaProductService.lambdaUpdate().eq(CmccDaProduct::getId, productId).set(CmccDaProduct::getDistanceFlowFlag, "0").update();
        return cmccDaAreaFlowService.removeByIds(idList) ? success() : error("删除失败");
    }

    /**
     * 删除分流号卡
     *
     * @param id 分流号卡id
     * @return 请求状态
     */
    @PostMapping("/delAreaFlowInfoById")
    AjaxResult delAreaFlowInfoById(@RequestBody Integer id) {
        return cmccDaAreaFlowService.removeById(id) ? success() : error("删除失败");
    }
}
